iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 14
0

https://ithelp.ithome.com.tw/upload/images/20200929/20113602IJC8g4MixK.jpg

Lightning 作為一個部落格平台,最重要的自然是發文功能。本篇要先準備好文章功能需要的 Migration、Model、Presenter 等等。

新增 Post Model 和相關檔案

為了快速產生需要的檔案,新增 Model 後面還帶上 -mfrm 是順便新增 Migration,f 是新增 Factory,r 是新增 Resource Controller,一行指令就解決:

php artisan make:model Post -mfr

然後規劃我們的資料表,這裡是用 author_id 指向 users 資料表:

database/migrations/2020_09_16_090156_create_posts_table.php

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('description');
    $table->text('content');
    $table->string('thumbnail')->nullable();
    $table->unsignedInteger('visits')->default(0);
    $table->boolean('published')->default(false);
    $table->foreignId('author_id')->constrained('users')->onDelete('cascade');
    $table->timestamps();
});

然後跑 Migrate:

php artisan migrate

這裡增加一些 Post 裡的設定,$fillable 是批量填充資料時允許的欄位;$casts 是型別轉換;updateDescription() 會自動更新 description,擷取文章內容的前80個字,並在新增和更新 Model 時擷取;還有關聯作者 User Model:

app/Post.php

use Illuminate\Support\Str;

protected $fillable = [
    'title', 'description', 'content', 'thumbnail', 'visits', 'published',
];

protected $casts = [
    'visits' => 'integer',
    'published' => 'boolean',
    'author_id' => 'integer',
];

protected static function booted()
{
    static::creating(function (self $post) {
        $post->updateDescription();
    });

    static::updating(function (self $post) {
        $post->updateDescription();
    });
}

public function updateDescription()
{
    $this->description = Str::limit(preg_replace('/\r|\n/', '', $this->content), 80);

    return $this;
}

public function author()
{
    return $this->belongsTo(User::class);
}

User 也要設定和 Post 的關聯,這裡也要設定對應的 key author_id

app/User.php

public function posts()
{
    return $this->hasMany(Post::class, 'author_id');
}

Post 的假資料 Factory:

database/factories/PostFactory.php

$factory->define(Post::class, function (Faker $faker) {
    return [
        'title' => $faker->realText(10),
        'content' => $faker->realText(1000),
        'visits' => $faker->numberBetween(10, 1000),
        'published' => $faker->boolean(80),
    ];
});

和假資料 Seeder:

php artisan make:seeder PostSeeder

隨機指派文章作者:

database/seeds/PostSeeder.php

use App\Post;
use App\User;

public function run()
{
    factory(Post::class, 12)->make()->each(function (Post $post) {
        User::inRandomOrder()->first()->posts()->save($post);
    });
}

database/seeds/DatabaseSeeder.php

public function run()
{
    ...
    $this->call(PostSeeder::class);
}

新增 PostPresenter

Presenter 也先準備好:

php artisan make:presenter PostPresenter

app/Presenters/PostPresenter.php

public function values(): array
{
    return [
        'id' => $this->id,
        'title' => $this->title,
        'description' => $this->description,
        'thumbnail' => $this->thumbnail,
        'visits' => $this->visits,
        'created_at' => $this->created_at->format('Y-m-d H:i:s'),
        'created_ago' => $this->created_at->diffForHumans(),
        'published' => $this->published,
    ];
}

總結

前置作業準備好,下一篇可以開始新增文章了!

Lightning 範例程式碼:https://github.com/ycs77/lightning


上一篇
Day 13 Lightning 刪除帳號 & 用戶頁面
下一篇
Day 15 Lightning 新增文章
系列文
關於我用 Laravel 寫 SPA 卻不寫 API 的那檔事30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言